import { observable, action ,runInAction,toJS} from "mobx";





// 评论数据
export default class Comment{
  
  // treeData
  @observable treeData=[];
  // 表单数据
  @observable form={insert:{},update:{}};
  // 选择上级分类
  @observable options=[];
  // 被选树形节点
  @observable info=false;
  // selectedKeys
  @observable selectedKeys=[];

  // paging 数据分页
  @observable paging={data:[]};
  // where查询条件
  @observable where={}; 
  // 查询分类
  @observable tid=0; 

  constructor(root){
    this.root = root;
  }
  
  @action init=()=>this._init();
  _init(){
    this._init_form();
    this.set("treeData",[]);
    this.set("options",[]);
    this.set("tid",0);

    this._init_paging();
    this._init_where();
    const tm = setInterval(()=>{
      if(this.root.website.sys && this.root.website.sys.productid){
        this._init_data();
        clearInterval(tm);
      }
    },100)
    this._paging(1);
  }

  @action set=(k,v)=>{
    const arr = k.split("-");
    const loop = (obj,arr,v)=>{
      const temp = arr.splice(0,1);
      if(arr.length){
        obj = obj[temp];
        loop(obj,arr,v);
      }else{
        obj[temp] = v;
      }
    };
    loop(this,arr,v);
  };
  
  // 加载子节点
  @action onLoadData = treeNode =>{
    return new Promise((resolve) => {
      if (treeNode.children) {
        resolve();
        return;
      }
      this.root.set("loading",true);
      this._find(treeNode.id,json=>{
        this.root.set("loading",false);
        if(json.status !== "ok"){
          this.root.modal.info({
            title:"提示",
            content:json.msg
          });
          resolve();
          return;
        }
        const children = json.data.map(
          item=>Object.assign(
            {
              key:item.id,
              title:item.tit,
              isLeaf: Number(item.num)===0
            },
            item
          )
        );
        treeNode.props.data.children=children;
        runInAction(()=>this.set("treeData",[...this.treeData]));
        resolve();
      });
    });
  };
  


  // 点击节点
  @action click = (keys,info)=>{
    this.set("selectedKeys",keys);
    if(!info.selected){
      this._init_form();
      this.info=false;
      return;
    }
    this.set("info",info);
    const dataRef = info.node.props.data;
    this.set("form-data-tid",dataRef.id);
    this.set("form-data-ttit",dataRef.tit);
  }

  // insert
  @action submit=()=>{
    this.root.set("loading",true);
    this.root.ajax.post({
      url:this.root.url,
      data:{
        act:"admin-link-".concat(this.form.condition),
        id:this.form.data.id,
        tit:this.form.data.tit,
        tid:this.form.data.tid,
        image:this.form.data.image,
        url:this.form.data.url,
        province:this.form.data.province,
        city:this.form.data.city,
        county:this.form.data.county,
        cont:this.form.data.cont
      },
      success:json=>{
        this.root.set("loading",false);
        this.root.alert(json);
        if(json.status !== "ok"){return;}
        switch(this.form.condition){
        case "update":
          this._paging(this.paging.nowPage);
          this.set("form-visible",false);
          break;
        case "insert":
        default:
          this.set("form-data-tit","");
          this.set("form-data-image","");
          this.set("form-data-url","");
          this.set("form-data-cont","");
          this._paging(this.paging.nowPage);
        }
      }
    });
  }

  // 上传图片
  @action uploadChange=(info,tp)=>{
    if(info.file.status === "uploading"){
      this.root.set("loading",true);
    }else if (info.file.status === "done"){
      this.root.set("loading",false);
      this.set("form-data-".concat(tp),info.file.response.data);
    }else if (info.file.status === "error") {
      this.root.alert({status:"error","msg":`${info.file.name} 图片上传失败!.`});
      this.root.set("loading",false);
    }else{
      this.root.set("loading",false);
    }
  }

  // delete
  @action delete=(id)=>{
    this.root.set("loading",true);
    this.root.ajax.post({
      url:this.root.url,
      data:{
        act:"admin-shops-delete",
        id:id
      },
      success:json=>{
        this.root.set("loading",false);
        this.root.alert(json);
        if(json.status === "ok"){
          this._paging(this.paging.nowPage);
        }
      }
    });
  }



  // 搜索
  @action search=()=>{

  }

  // 分页
  @action page=p=>{
    this._paging(p);
  }

  // 评论分页数据
  _paging(p){
    this.root.set("loading",true);
    p = p?p:1;
    this.root.ajax.post({
      url:"".concat(this.root.url2,"/product-comment/page"),
      data:{
        act:"admin-shops-comment",
        p:p,
        field:this.where.field,
        value:this.where.value,
        condition:this.where.condition
      },
      success:json=>{
        this.root.set("loading",false);
        if(json.status !== "ok"){this.root.alert(json);return;}
        this.set("paging",json.data);
      }
    });
  }
  
  
  // 初始化树形分类
  _init_data(){
    this._find(this.root.website.sys.productid,json=>{
      // console.log(json);  
      this.root.set("loading",false);
      if(json.status !== "ok"){this.root.alert(json);return;}
      const treeData = json.data.map(item=>Object.assign({key:item.id,title:item.tit,isLeaf: item.num==="0"},item));
      // this.set("treeData",treeData);
      this.set("treeData",[{tit:"商品分类",id:this.root.website.sys.productid,upid:0,cont:"商品分类",title:"商品分类",key:this.root.website.sys.productid,children:treeData}]);
      
      const options=json.data.map(item=>Object.assign({value:item.id,title:item.tit,isLeaf: item.num==="0"},item));
      // this.set("options",options);
      this.set("options",[{label:"商品分类",value:this.root.website.sys.productid,children:options}]);
    });
  }
  
  // 初始化评论数据
  @action init_form=()=>{
    this._init_form();
  }
  _init_form(){
    this.set("form",{
      data:{
        id:0,
        province:0,
        city:0,
        district:0,
        checked:0,
        tit:"",
        note:"",
        address:"",
        image:""
      }
    });
  }
  // 分页数据初始化
  _init_paging(){
    this.set("paging",{
      data:[],
      maxPage:1,
      page :1,
      maxNum:1,
      num:20,
    });
  }

  // 查询表单初始化
  _init_where(){
    this.set("where",{
      field:"",
      value:"",
      condition:""
    });
  }
  
  // 获取分类数据
  _find(upid,func){
    this.root.set("loading",true);
    this.root.ajax.post({
      url:this.root.url,
      data:{act:"admin-type-select",upid:upid},
      success:func
    });
  }

  
  // 加载下拉菜单
  @action onLoadOption = selectedOptions => this._onLoadOption(selectedOptions);
  _onLoadOption(selectedOptions) {
    const targetOption = selectedOptions[selectedOptions.length - 1];
    targetOption.loading = true;
    this.root.set("loading",true);
    this._find(targetOption.id,json=>{
      targetOption.loading = false;
      this.root.set("loading",false);
      if(json.status !== "ok"){
        this.root.modal.info({
          title:"提示",
          content:json.msg
        });
        return;
      }
      if(json.data.length){
        runInAction(()=>{
          targetOption.children = json.data.map(item=>Object.assign({value:item.id,label:item.tit,isLeaf: item.num==="0",},item));  
        });
        runInAction(()=>this.set("options",[...this.options]));
      }
    });
  }

  

  

}